// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   initDB.java

package include.auto_execute;

import include.data_backup.Solid;
import include.nseer_db.nseer_dbb;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import javax.servlet.ServletContext;

public class initDB
{

	public initDB()
	{
	}

	public void initDB(ServletContext dbApplication)
	{
		try
		{
			String path = dbApplication.getRealPath("/");
			String filename = (new StringBuilder(String.valueOf(path))).append("WEB-INF/dynamic_backup.xml").toString();
			Solid so = new Solid(filename);
			for (int j = 1; j < 3; j++)
			{
				String db = (new StringBuilder("ondemand")).append(j).toString();
				String status = so.getValue(db);
				if (status != null && status.equals("1"))
				{
					nseer_dbb dbb1_backup = new nseer_dbb("db_backup.properties", "dbip_backup.properties", "mysql");
					nseer_dbb dbb = new nseer_dbb("db.properties", "dbip.properties", db);
					nseer_dbb dbb1 = new nseer_dbb("db.properties", "dbip.properties", db);
					nseer_dbb dbb_backup = new nseer_dbb("db_backup.properties", "dbip_backup.properties", db);
					String sqld = (new StringBuilder("drop database IF EXISTS ")).append(db).toString();
					dbb1_backup.executeUpdate(sqld);
					sqld = (new StringBuilder("CREATE DATABASE ")).append(db).toString();
					dbb1_backup.executeUpdate(sqld);
					System.out.println((new StringBuilder("create database ")).append(db).append(" ok").toString());
					String sql = "show tables";
					String column = (new StringBuilder("tables_in_")).append(db).toString();
					for (ResultSet rs = dbb.executeQuery(sql); rs.next();)
						if (!rs.getString(column).equals("dynamic_backup"))
						{
							System.out.println((new StringBuilder("sync ")).append(rs.getString(column)).append(" data").toString());
							String sql2 = (new StringBuilder("DESCRIBE ")).append(rs.getString(column)).toString();
							ResultSet rs1 = dbb1.executeQuery(sql2);
							String sqlt = (new StringBuilder("CREATE TABLE IF NOT EXISTS ")).append(rs.getString(column)).append(" (id int(10) unsigned NOT NULL auto_increment,").toString();
							rs1.next();
							while (rs1.next()) 
								sqlt = (new StringBuilder(String.valueOf(sqlt))).append(rs1.getString("Field")).append(" ").append(rs1.getString("Type")).append(" NOT NULL DEFAULT '").append(rs1.getString("Default")).append("',").toString();
							sqlt = (new StringBuilder(String.valueOf(sqlt))).append("PRIMARY KEY (id))").toString();
							dbb_backup.executeUpdate(sqlt);
							sql2 = (new StringBuilder("select * from ")).append(rs.getString(column)).toString();
							rs1 = dbb1.executeQuery(sql2);
							ResultSetMetaData rsmd = rs1.getMetaData();
							int number = rsmd.getColumnCount();
							String sql5;
							for (; rs1.next(); dbb_backup.executeUpdate(sql5))
							{
								String sql3 = "";
								String sql4 = "";
								for (int i = 1; i < number; i++)
								{
									sql3 = (new StringBuilder(String.valueOf(sql3))).append(rsmd.getColumnName(i)).append(",").toString();
									sql4 = (new StringBuilder(String.valueOf(sql4))).append("'").append(rs1.getString(rsmd.getColumnName(i))).append("',").toString();
								}

								sql3 = (new StringBuilder(String.valueOf(sql3))).append(rsmd.getColumnName(number)).toString();
								sql4 = (new StringBuilder(String.valueOf(sql4))).append("'").append(rs1.getString(rsmd.getColumnName(number))).append("')").toString();
								sql5 = (new StringBuilder("insert into ")).append(rs.getString(column)).append("(").append(sql3).append(") values(").append(sql4).toString();
							}

							System.out.println((new StringBuilder("sync ")).append(rs.getString(column)).append(" data ok!").toString());
						}

					so.update(db, "1", "0");
					dbApplication.setAttribute((new StringBuilder(String.valueOf(db))).append("sync").toString(), "ok");
					dbApplication.setAttribute("mysqlsync", "ok");
					dbb.close();
					dbb1.close();
					dbb_backup.close();
					dbb1_backup.close();
				} else
				if (status != null && status.equals("2"))
				{
					nseer_dbb dbb1_backup = new nseer_dbb("db_backup.properties", "dbip_backup.properties", "mysql");
					nseer_dbb dbb = new nseer_dbb("db.properties", "dbip.properties", db);
					nseer_dbb dbb1 = new nseer_dbb("db.properties", "dbip.properties", db);
					nseer_dbb dbb_backup = new nseer_dbb("db_backup.properties", "dbip_backup.properties", db);
					String sqld = (new StringBuilder("drop database IF EXISTS ")).append(db).toString();
					dbb1_backup.executeUpdate(sqld);
					sqld = (new StringBuilder("CREATE DATABASE ")).append(db).toString();
					dbb1_backup.executeUpdate(sqld);
					System.out.println((new StringBuilder("create database")).append(db).append(" ok").toString());
					String sql = "show tables";
					String column = (new StringBuilder("tables_in_")).append(db).toString();
					for (ResultSet rs = dbb.executeQuery(sql); rs.next();)
						if (!rs.getString(column).equals("dynamic_backup"))
						{
							System.out.println((new StringBuilder("sync ")).append(rs.getString(column)).append(" data").toString());
							String sql2 = (new StringBuilder("DESCRIBE ")).append(rs.getString(column)).toString();
							ResultSet rs1 = dbb1.executeQuery(sql2);
							String sqlt = (new StringBuilder("CREATE TABLE IF NOT EXISTS ")).append(rs.getString(column)).append(" (id int(10) unsigned NOT NULL auto_increment,").toString();
							rs1.next();
							while (rs1.next()) 
								sqlt = (new StringBuilder(String.valueOf(sqlt))).append(rs1.getString("Field")).append(" ").append(rs1.getString("Type")).append(" NOT NULL DEFAULT '").append(rs1.getString("Default")).append("',").toString();
							sqlt = (new StringBuilder(String.valueOf(sqlt))).append("PRIMARY KEY (id))").toString();
							dbb_backup.executeUpdate(sqlt);
							sql2 = (new StringBuilder("select * from ")).append(rs.getString(column)).toString();
							rs1 = dbb1.executeQuery(sql2);
							ResultSetMetaData rsmd = rs1.getMetaData();
							int number = rsmd.getColumnCount();
							String sql5;
							for (; rs1.next(); dbb_backup.executeUpdate(sql5))
							{
								String sql3 = "";
								String sql4 = "";
								for (int i = 1; i < number; i++)
								{
									sql3 = (new StringBuilder(String.valueOf(sql3))).append(rsmd.getColumnName(i)).append(",").toString();
									sql4 = (new StringBuilder(String.valueOf(sql4))).append("'").append(rs1.getString(rsmd.getColumnName(i))).append("',").toString();
								}

								sql3 = (new StringBuilder(String.valueOf(sql3))).append(rsmd.getColumnName(number)).toString();
								sql4 = (new StringBuilder(String.valueOf(sql4))).append("'").append(rs1.getString(rsmd.getColumnName(number))).append("')").toString();
								sql5 = (new StringBuilder("insert into ")).append(rs.getString(column)).append("(").append(sql3).append(") values(").append(sql4).toString();
							}

							System.out.println((new StringBuilder("sync ")).append(rs.getString(column)).append(" data ok!").toString());
						}

					String sql6 = "CREATE TABLE IF NOT EXISTS dynamic_backup (id int(10) unsigned NOT NULL auto_increment,name varchar(30) NOT NULL DEFAULT '',PRIMARY KEY (id))";
					dbb.executeUpdate(sql6);
					sql6 = "insert into dynamic_backup (name) values('master')";
					dbb.executeUpdate(sql6);
					so.update(db, "1", "0");
					dbApplication.setAttribute((new StringBuilder(String.valueOf(db))).append("sync").toString(), "ok");
					dbApplication.setAttribute("mysqlsync", "ok");
					dbb.close();
					dbb1.close();
					dbb_backup.close();
					dbb1_backup.close();
				}
			}

		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
